﻿using System;
using System.ComponentModel;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Win.Templates.ActionContainers;
using DevExpress.XtraBars;
using DevExpress.ExpressApp;
using System.Windows.Forms;
using Hdt.TransOcean.Dal;
using System.Collections.Generic;
using DevExpress.XtraGrid;

namespace Hdt.TransOcean.Presentation
{
    public partial class FormAirlineList : DevExpress.ExpressApp.Win.Templates.XtraFormTemplateBase, ISupportClassicToRibbonTransform
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            CheckTransformToRibbon();
        }
        protected override IModelFormState GetFormStateNode()
        {
            if (View != null)
            {
                return TemplatesHelper.GetFormStateNode(View.Id);
            }
            else
            {
                return base.GetFormStateNode();
            }
        }
        public override void SetSettings(IModelTemplate modelTemplate)
        {
            base.SetSettings(modelTemplate);
            formStateModelSynchronizerComponent.Model = GetFormStateNode();
        }
        [Obsolete("Use the DetailViewForm() constructor"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public FormAirlineList(XafApplication application) : this() { }
        public FormAirlineList()
        {
            InitializeComponent();
        }

        private void hdtManageControl1_HdtSaveButtonClick()
        {
            MessageBox.Show("TEst" );
            
        }

        private void gridViewAirline_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            //DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            //MessageBox.Show(e.RowHandle.ToString());
            //view.SetRowCellValue(e.RowHandle, view.Columns["gridColumnCode"], "Input data");
            //view.SetRowCellValue(e.RowHandle, view.Columns["gridColumnName"], "Input data");
            //view.SetRowCellValue(e.RowHandle, view.Columns["gridColumnDescription"], "Input data");
        }

        private void hdtManageControl1_HdtNewButtonClick()
        {
            Presentation.OpenForm(MdiParent, "FormAirline", EOperation.New);
        }

        private void FormAirlineList_Load(object sender, EventArgs e)
        {
            BindAirlineGrid();
        }

        private void BindAirlineGrid()
        {
            BindingSource bsAirline = new BindingSource();

            var airlines = Dao.SelectAirline();
            bsAirline.DataSource = airlines;
            gridControlAirline.DataSource = bsAirline;
            gridColumnCode.FieldName = "code";
            gridColumnName.FieldName = "name";
            gridColumnDescription.FieldName = "description";
        }

        private void gridViewAirline_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Tab)
            { 
                
            
            
            }
        }

        private void hdtManageControl1_HdtExportButtonClick()
        {
            Dao dao = new Dao();
            bool flag = true;
            List<string[]> rows = new List<string[]>();
                       
            var items = Dao.SelectAirline();

            foreach (Airline a in items)
            {
                rows.Add(new string[] { a.code.ToString(), a.name.ToString(), a.description.ToString() });
            }

            try
            {
                flag = Dao.ExportExcel(EObject.Airline, "Airline.xls", rows);
            }
            catch (Exception e)
            {
                flag = false;
                MessageBox.Show(e.ToString());
            }
            finally
            {
                if(flag)
                    MessageBox.Show("Export thành công!","Hãng hàng không");
            }
            
        }

        private void hdtManageControl1_HdtImportButtonClick()
        {
            bool flag = true;
            
            try
            {
                flag = Dao.ImportExcel(EObject.Airline);
            }
            catch (Exception e)
            {
                flag = false;
                MessageBox.Show(e.ToString());
            }
            finally
            {
                if (flag)
                    MessageBox.Show("Import thành công!", "Hãng hàng không");
            }
        }

        private void hdtManageControl1_HdtDeleteButtonClick()
        {
            Dao dao = new Dao();
            
            string code = "";
            if (gridViewAirline.FocusedRowHandle != -1)
            {
                code = gridViewAirline.GetRowCellValue(gridViewAirline.FocusedRowHandle, "code").ToString();
            }
            try
            {
                if (MessageBox.Show("Bạn muốn xóa không?", "Hãng hàng không", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK)
                {
                    if (Dao.DeleteAirlineByCode(code) == true)
                    {
                        BindingSource bsAirline = new BindingSource();

                        var airlines = Dao.SelectAirline();
                        bsAirline.DataSource = airlines;
                        gridControlAirline.DataSource = bsAirline;
                        gridColumnCode.FieldName = "code";
                        gridColumnName.FieldName = "name";
                        gridColumnDescription.FieldName = "description";
                        
                        MessageBox.Show("Xóa thành công", "Hãng hàng không");
                    }
                }
            }
            catch (Exception e) {

                MessageBox.Show(e.ToString());
            }
        }

        private void gridViewAirline_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.RowHandle >= 0 && e.RowHandle != null)
            {
                MessageBox.Show(e.RowHandle.ToString());
            }
        }

        private void gridViewAirline_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                MessageBox.Show(e.RowHandle.ToString());
            }
        }

        private void gridViewAirline_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            MessageBox.Show(e.ControllerRow.ToString());
        }
        
        private void gridViewAirline_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            Presentation.DisplayButtonByFocusedRowHandle(hdtManageControl1, e.FocusedRowHandle);
        }

        string temp;
        private void gridViewAirline_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            hdtManageControl1.EnabledSaveButton = true;
            temp = e.Value.ToString();
            
        }

        private void gridViewAirline_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            hdtManageControl1.EnabledSaveButton = false;
        }

        private void hdtManageControl1_HdtEditButtonClick()
        {
            string code = "";

            if (gridViewAirline.FocusedRowHandle != -1)
            {
                code = gridViewAirline.GetRowCellValue(gridViewAirline.FocusedRowHandle, "code").ToString();
                Presentation.OpenForm(MdiParent, "FormAirline", EOperation.Edit, code);
            }
        }

        private void hdtManageControl1_HdtCloseButtonClick()
        {
            this.Close();
        }
        
    }
}
